# -*- coding: utf-8 -*-

from __future__ import with_statement

from dicthash.for_testing import TestGoogleDataStore, in_namespace, captured_templates
from google.appengine.ext import db
from datetime import datetime
from werkzeug.urls import url_decode
from urlparse import urlparse

from lenta.code.backends.working.models import Lenta, Note
from application.app import app


class TestInjectIntoTemplate(TestGoogleDataStore):
    """ """
    def test(self):
        """ """
        res = app.test_client().get('/lenta/')
        self.assertEqual(res.status_code, 200)
        
    def test_lenta(self):
        """ """
        with in_namespace("testusername"):
            self.lenta = Lenta.new(name=u"Дневник ебанько", desc=u"Эту книгу ждали 8 лет...")
            self.lenta.put()
        with captured_templates(app) as tmpl_info:
            res = app.test_client().get('/lenta/testusername/%s/info' % self.lenta.id)
            self.assertEqual(res.status_code, 200)
            self.assertTrue('c' in tmpl_info[0][1])
            c = tmpl_info[0][1]['c']
            self.assertEqual(c['lenta'].name, self.lenta.name)
            
            
class Test_get_notes_by_tag(TestGoogleDataStore):
    """
    Testing `get_notes_by_tag` view.
    """
    def setUp(self):
        """ """
        super(Test_get_notes_by_tag, self).setUp()
        self.dttm_list = [
                    datetime(2012, 1, 31), # 0
                    datetime(2012, 1, 30), #
                    datetime(2012, 1, 29), # 2
                    datetime(2012, 1, 28), #
                    datetime(2012, 1, 27), # 4
                    datetime(2012, 1, 26), #
                    datetime(2012, 1, 25), # 6
                    datetime(2012, 1, 24), #
                    datetime(2012, 1, 23), # 8
                    datetime(2012, 1, 22), #
                    datetime(2012, 1, 21), # 10
                    datetime(2012, 1, 20), #
                    datetime(2012, 1, 19), # 12
                    datetime(2012, 1, 18)  #
        ]
        self.tag_list = [
                    ['firebird'], # 0
                    ['firebird'], #
                    ['firebird'], # 2
                    ['sadko'],    #
                    ['firebird'], # 4
                    ['firebird'], #
                    ['firebird'], # 6
                    ['firebird'], #
                    ['mlada'],    # 8
                    ['firebird'], #
                    ['firebird'], # 10
                    ['firebird'], #
                    ['firebird'], # 12
                    ['firebird'], #
        ]
        
        self.namespace = 'testnamespace'
        with in_namespace(self.namespace):
            self.lenta_key = Lenta.new(name="A", desc="A B C").put()
            
            note_kwargs = dict(title="Boom!", twit="That's boom",
                               body="Boom-boom-boom.", parent=self.lenta_key)
            
            db.put([Note.new(pubDate=dttm, tags=tags, **note_kwargs)
                    for dttm, tags in zip(self.dttm_list, self.tag_list)])
    
    def test(self):
        """ """
        with captured_templates(app) as tmpl_info:
            res = app.test_client().get("/lenta/%s/%s/note/tag/%s?limit=10" % \
                                                    (self.namespace, self.lenta_key.id(), 'firebird'))
            
            self.assertEqual(res.status_code, 200)
            self.assertEqual(tmpl_info[0][0], "/lenta/note/notes_by_tag.html")
            self.assertEqual(len([_ for _ in tmpl_info[0][1]['notes']]), 10)
            
            self.assertEqual(url_decode(urlparse(tmpl_info[0][1]['next_page_url']).query, cls=dict),
                             {'limit':'10', 'dttm':'2012-01-20-00-00-00'})          